import {USER_LOGIN,INIT_USER} from './mutations-types'
export default {
    state: {
        loginStatus: false, //登录状态
        token: null,  //token
        userInfo:{},  //用户信息
    },

    getters: {
        
    },

    mutations: {
        [USER_LOGIN](state, user) {
            state.loginStatus = true;
            state.token = user.token;
            state.userInfo = user;

            //持久化存储，本地存储，刷新页面不用重新登录
            localStorage.setItem('teaUserInfo', JSON.stringify(user));
         
        },
        //读取本地存储的用户信息
        [INIT_USER](state) {
            let userInfo = JSON.parse(localStorage.getItem('teaUserInfo'))
            if (userInfo) {
                state.loginStatus = true;
                state.token = userInfo.token;
                state.userInfo = userInfo;
            }
        },
        //退出登录
        loginOut(state) {
            state.loginStatus = false;
            state.token = null;
            state.userInfo = {};
            localStorage.removeItem('teaUserInfo')
        },

    },

    actions: {
        
    },

}